package com.ddstation.onsitelaw.service;


import com.ddstation.common.util.IdGenerateUtil;
import com.ddstation.onsitelaw.entity.DtInvsPrescriptionManualAuditEntity;
import com.ddstation.onsitelaw.entity.DtInvsViolationManageEntity;
import com.ddstation.onsitelaw.entity.MaInvsViolationMessageEntity;
import com.ddstation.onsitelaw.mapper.AuditPrescriptionMapper;
import com.ddstation.onsitelaw.mapper.DtInvsManageInstitutionSupervisorMapper;
import com.ddstation.onsitelaw.mapper.MaInvsViolationMessageMapper;
import com.ddstation.onsitelaw.mapper.ViolationRecordMapper;
import com.ddstation.onsitelaw.model.AccountModel;
import com.ddstation.onsitelaw.model.AuditPrescriptionModel;
import com.ddstation.onsitelaw.model.ViolationManageModel;
import com.ddstation.onsitelaw.utils.AliCloudOssUtil;
import com.ddstation.onsitelaw.utils.DateUtil;
import com.ddstation.onsitelaw.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ViolationRecordServiceImpl implements ViolationRecordService {
    @Inject
    private ViolationRecordMapper violationRecordMapper;
    @Inject
    private MaInvsViolationMessageMapper maInvsViolationMessageMapper;

    @Inject
    private AuditPrescriptionMapper auditPrescriptionMapper;


    @Inject
    private DtInvsManageInstitutionSupervisorMapper dtInvsManageInstitutionSupervisorMapper;

    @Override
    @Transactional(readOnly = true)
    public PageResult selectViolationRecord(String county, Integer pageNum, Integer pageSize, List<String> institutionIds,Integer uploaderId) {
        //查询ma_invs_violation_message，获取title
        List<MaInvsViolationMessageEntity> msgList = maInvsViolationMessageMapper.selectList();
        Map<String, String> msgTitleMap = msgList.stream().collect(Collectors.toMap(MaInvsViolationMessageEntity::getMsgId, MaInvsViolationMessageEntity::getTitle));

        //获取处方审核图片
        Example auditPrescriptionExample = new Example(DtInvsPrescriptionManualAuditEntity.class);
        Example.Criteria auditPrescriptionCriteria = auditPrescriptionExample.createCriteria();
        if (!ObjectUtils.isEmpty(institutionIds)) {
            auditPrescriptionCriteria.andIn("institutionId", institutionIds);
        }
        List<DtInvsPrescriptionManualAuditEntity> auditPrescriptionList = auditPrescriptionMapper.selectByExample(auditPrescriptionExample);
        Map<String, DtInvsPrescriptionManualAuditEntity> auditPrescriptionListMap = auditPrescriptionList.stream().collect(Collectors.toMap(DtInvsPrescriptionManualAuditEntity::getMedicalId, Function.identity()));
        //分页插件，紧跟查询之前。
        HashMap hashMap = new HashMap();
        hashMap.put("institutionIds", institutionIds);
        hashMap.put("county", county);
        hashMap.put("uploaderId", uploaderId);
        PageHelper.startPage(pageNum, pageSize);
        List<ViolationManageModel> violationRecordEntityList = violationRecordMapper.selectViolationList(hashMap);
        PageInfo<ViolationManageModel> info = new PageInfo<>(violationRecordEntityList);

        violationRecordEntityList.stream().forEach(item -> {
            item.setUploadTmStr(DateUtil.getStringDateShort(item.getUploadTm()));
            item.setTitle(msgTitleMap.get(item.getMsgId()));
            item.setViolateDtStr(DateUtil.getStringDateShort(item.getViolateDt()));
            DtInvsPrescriptionManualAuditEntity auditEntity = auditPrescriptionListMap.get(item.getMedicalId().toString());
            if (!ObjectUtils.isEmpty(auditPrescriptionListMap) && !ObjectUtils.isEmpty(auditEntity)) {
                if (item.getPrescriptionId().equals(auditEntity.getPrescriptionId())) {
                    item.setPrescriptionPicUrl(AliCloudOssUtil.getOriginImgUrl(null, auditEntity.getPrescriptionPicUrl()));
                }
            }
        });

        return new PageResult(info, violationRecordEntityList);
    }


    @Override
    @Transactional(readOnly = true)
    public PageResult manageViolationList(List<String> institutionIds, String county, Integer pageNum, Integer pageSize) {
        //分页插件，紧跟查询之前。
        HashMap map = new HashMap();
        map.put("institutionIds", institutionIds);
        map.put("county", county);
        PageHelper.startPage(pageNum, pageSize);
        List<ViolationManageModel> violationRecordEntityList = violationRecordMapper.manageViolationList(map);
        PageInfo<ViolationManageModel> info = new PageInfo<>(violationRecordEntityList);
        return new PageResult(info, violationRecordEntityList);
    }


    @Override
    @Transactional(readOnly = true)
    public List<MaInvsViolationMessageEntity> selectList() {
        return maInvsViolationMessageMapper.selectList();
    }

    @Override
    @Transactional(readOnly = true)
    public PageResult selectViolationListForPC(HashMap paramMap) {
        //查询ma_invs_violation_message，获取title
        List<MaInvsViolationMessageEntity> msgList = maInvsViolationMessageMapper.selectList();
        Map<String, String> msgTitleMap = msgList.stream().collect(Collectors.toMap(MaInvsViolationMessageEntity::getMsgId, MaInvsViolationMessageEntity::getTitle));

        List<AccountModel> accountList = dtInvsManageInstitutionSupervisorMapper.selectAllSupervisors();
        Map<String, String> accountMap = accountList.stream().collect(Collectors.toMap(AccountModel::getLoginUser, AccountModel::getName));


        //分页插件，紧跟查询之前。
        PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()), Integer.parseInt(paramMap.get("pageSize").toString()));
        List<ViolationManageModel> violationRecordEntityList = violationRecordMapper.selectViolationListForPC(paramMap);
        PageInfo<ViolationManageModel> info = new PageInfo<>(violationRecordEntityList);

        violationRecordEntityList.stream().forEach(item -> {
            item.setTitle(msgTitleMap.get(item.getMsgId()));
            item.setUploaderName(accountMap.get(item.getUploaderName()));
            item.setAuditTmStr(DateUtil.getStringDateShort(item.getAuditTm()));
            item.setUploadTmStr(DateUtil.getStringDateShort(item.getUploadTm()));
            item.setPrescriptionPicUrl(AliCloudOssUtil.getOriginImgUrl(null, item.getPrescriptionPicUrl()));
        });
        return new PageResult(info, violationRecordEntityList);
    }


    @Override
    public void insertViolation(AuditPrescriptionModel auditPrescriptionModel) {
        DtInvsViolationManageEntity entity = new DtInvsViolationManageEntity();
        entity.setInvsInstitutionId(auditPrescriptionModel.getInvsInstitutionId());
        entity.setInstitutionId(auditPrescriptionModel.getInstitutionId());
        entity.setMedicalId(auditPrescriptionModel.getMedicalId());
        entity.setPrescriptionId(auditPrescriptionModel.getPrescriptionId());
        entity.setViolateDt(auditPrescriptionModel.getViolateDt());
        entity.setMsgId(auditPrescriptionModel.getMsgId());
        entity.setMsgTxt(auditPrescriptionModel.getMsgTxt());
        entity.setSourceType(4);
        entity.setId(IdGenerateUtil.idGenerateFromTime("04"));
        violationRecordMapper.insertSelective(entity);
    }
}